<?php
namespace app\index\model;
use think\Model;

class OrderNum extends Model{

    /**
     * 获取交期变更单列表
     */
    public function getDeliveryList($page = '', $limit = '', $orderNo = '', $customerName = '', $uid, $type){
        //销售-》计划，取order表里面atUser等于自己的数据;后面需求改为销售人员可以变更别人的订单
        //计划-》销售，取order表里面所有用户的
//        if ($type == 1){
//            $where = " a.deleted = 0 and b.deleted = 0 and b.atUser = $uid and b.atTime > '2021-01-01 00:00:00' ";
//        } else {
            $where = " a.deleted = 0 and b.deleted = 0 and b.atTime > '2021-01-01 00:00:00' ";
//        }
        if ($orderNo) {
            $where .= " and concat_ws('',a.num, a.desc) = '".$orderNo."'";
        }
        if ($customerName) {
            $where .= " and a.cCusName = '".$customerName."'";
        }
        return db('ordernum')
                ->alias('a')
                ->join('order b','a.status = b.id')
                ->join('(SELECT orderId, sum(iFHQuantity) as quantitySum from dev_orderlist GROUP BY orderId HAVING quantitySum = "0.00") c', 'b.id = c.orderId')
                ->where($where)
                ->field('a.id, a.num, a.ddLeiXing, a.orderType, a.desc, a.zhangtao, b.currency, a.cCusName, a.cCusPPersonText, b.date1, b.date2, b.is_show as isShow, b.atTime, b.id as orderId')
                ->limit(($page-1)*$limit, $limit)
                ->select();
    }

    public function count($orderNo = '', $customerName = '', $uid, $type){
        //销售-》计划，取order表里面atUser等于自己的数据
        //计划-》销售，取order表里面所有用户的
//        if ($type == 1){
//            $where = " a.deleted = 0 and b.deleted = 0 and b.atUser = $uid and b.atTime > '2021-01-01 00:00:00' ";
//        } else {
            $where = " a.deleted = 0 and b.deleted = 0 and b.atTime > '2021-01-01 00:00:00' ";
//        }
        if ($orderNo) {
            $where .= " and concat_ws('',a.num, a.desc) = '".$orderNo."'";
        }
        if ($customerName) {
            $where .= " and a.cCusName = '".$customerName."'";
        }
        return db('ordernum')
                ->alias('a')
                ->join('order b','a.status = b.id')
                ->join('(SELECT orderId, sum(iFHQuantity) as quantitySum from dev_orderlist GROUP BY orderId HAVING quantitySum = "0.00") c', 'b.id = c.orderId')
                ->where($where)
                ->count();
    }

     /**
     * 账套列别
     */
    public function findZhangTao($fName, $value){
        return db('field')->where(['fName'=>$fName, 'value'=>$value, 'deleted'=>0])->find();
    }

    /**
     * 获取本订单出了哪些产品
     */
    public function getProductsFromOrder($orderId){
        $categorys = '';
        $temp = db("orderlist")->where(['orderId'=>$orderId, 'deleted'=>0])->group("category")->select();
        if(!empty($temp)){
            foreach($temp as $temp2){
                $categorys .= $temp2['category'].'|';
            }
        }
        return $categorys;
    }

    // public function getCustomer(){
    //     // cCusCode
    //     $temp = db("orderlist")->where(['orderId'=>$orderId, 'deleted'=>0])->group("category")->select();
    // }

}
?>